<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Model_Member extends __Model {

	private $blowfish	= null;
	
	private $member		= null;
	
	function __construct() {

		parent::__construct();

		$this->load->module("mysql", $this->db );
		$this->load->model("admin/admin", $this->admin );

		$this->blowfish = $this->admin->blowfish;

	}

	function auth( $level = 0 ) { // level limit
		/*
		$mylevel = 10;

		if ( isset($_SESSION[$this->blowfish."_mem"]) ) {
			$member = $this->getMember();
			if ( isset( $member['level']) ) $mylevel = $member['level'];
		}
		
		if ($mylevel > $level) return false;
		else 
		*/
		return true;

	}


	function getMember() {
		static $ret;
		
		if ( isset($ret) ) return $ret;
		
		if ( !isset($_SESSION[$this->blowfish."_mem"]) ) return null;
		
		$member = $this->admin->memberDecode( $_SESSION[$this->blowfish."_mem"] );
		
		$member = unserialize($member);
		if ($member["idx"] > 0 ) $ret = $member;
		else $ret = null;
		
		return $ret;
	}

	function login($id, $pw) {
		$pw = $this->admin->password($pw);
		$member = $this->db->result("select * from `wd_member` where `id`='{$id}' and `password`='{$pw}'");
		$member['log_date'] = time();

		if ( isset($member['idx']) && $member['idx'] > 0 ) {
			$_SESSION[$this->blowfish."_mem"] = $this->admin->memberEncode(serialize( $member ));
			return 1;
		}
		else {
			$this->logout();
			return 0;
		}
	}
	function logout() {
		session_destroy();
		$_SESSION[$this->blowfish."_mem"] = $this->admin->memberEncode(serialize( array("idx" => 0) ));
		return 1;
	}


// End Class
}
